/**
 * Created by Administrator on 2018/5/4.
 */
var sTp2;
define(function (require,exports,module) {
    require("./schedule.css");
    // require("../../../lib/jquery/jquery-ui.css")
    // require("../../../lib/jquery/jquery-ui.min.js")
    // require("../../../lib/jquery.dragsort-0.5.2.min.js")
    var sTpl = require("./template.html");

    var VueComponent = Vue.extend({
        props:['parentData'],
        template: sTpl,
        methods:{
            edit:function(scope){
                scope.edit=true;
            },
            chose:function (room,index) {
                if(room.roomStatus==1&&this.parentData!=null){
                    this.chooseRoomId=room.roomId;
                    this.parentData.selectFunction(room);
                }
            },
            cancleEdit:function (scope,cancleUpdate) {
                if(cancleUpdate){
                    this.$refs[scope.gsiId][0].resetFields();
                }
                scope.edit=false;
            },searchAll:function(){
                var $this = this;
                axios.get(basepath+'/api/flightPlane/queryAllScheduleItem'+this.filter)
                    .then(function (response) {
                        response.data.forEach(function (data,i) {
                            data.edit=false;
                        })
                        $this.serviceItems=response.data;
                    });
            },deleteRoom:function (scope,index) {
                var $this=this;
                this.$confirm('删除后将无法恢复', '确定删除该服务项？', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/flightPlane/deleteScheduleItem',scope)
                        .then(function (response) {
                            $this.serviceItems.splice(index,1);
                            $this.$notify({
                                title: '删除成功',
                                message: '房间已删除'
                            });
                        });
                })
            }
            ,addItems:function () {
                var $this=this;
                this.$refs.addItem.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/flightPlane/saveScheduleItem',$this.addItem)
                            .then(function (response) {
                                response.data.edit=false;
                                $this.serviceItems.unshift(response.data);
                                $this.$refs["addItem"].resetFields();
                                $this.addItem.edit=false;
                                $this.$notify({
                                    title: '添加成功',
                                    message: '添加成功'
                                });
                            });
                    }
                });
            },

            update:function (scope,validate) {
                var $this=this;
                if(validate){
                    if(scope.scheduleBaseNum==1){
                        scope.hasGroup=false;
                    }
                    this.$refs[scope.gsiId][0].validate(function(validate){
                        if(validate){
                            axios.post(basepath+'/api/flightPlane/saveScheduleItem',scope)
                                .then(function (response) {
                                    scope.edit=false;
                                    $this.$notify({
                                        title: '更新成功',
                                        message: '更新成功'
                                    });
                                });
                        }
                    });
                }
            },
            selectGroup:function (value) {
                var $this= this;
                this.currentService.selectAbleUser.forEach(function (data,i) {
                    if(data.serviceGroup==value){
                        data.main=false;
                        $this.$set($this.currentService.selectAbleUser,i,data)
                    }
                })
            },
            addUser:function (scope) {
                this.currentService = scope;
                var $this= this;
                this.transferResult=this.currentService.teams;
                this.allTeams = this.allUser;
                // var map = {};
                // this.serviceItems.forEach(function (data) {
                //     if(data.gsiId!=$this.currentService.gsiId){
                //         if(data.teams){
                //             data.teams.forEach(function (da) {
                //                 map[da.teamId]=true;
                //             })
                //         }
                //
                //     }
                // })
                // this.allTeams = this.allUser.filter(function (data) {
                //     if(!map[data.teamId])return data;
                // });
                // this.currentService.selectAbleUser.forEach(function(data){
                //     // data.selectAbleUser.selectableUsersId = data.selectableUsersId;
                //     // data.selectAbleUser.serviceTime = data.serviceTime;
                //     $this.transferResult.push(data.selectAbleUser)
                // });
                this.dialog=true;
            },
            addUserComplate:function () {
                var $this = this;
                this.dialog=false;
                if(this.currentService.scheduleBaseNum==1){
                    this.currentService.hasGroup=false;
                }
                if(this.currentService.teams.length>0){
                    axios.post(basepath+'/api/flightPlane/saveScheduleItemTeam',this.currentService)
                        .then(function (response) {
                            $this.dialog=false;
                            $this.$notify({
                                title: '配置成功',
                                message: '配置成功'
                            });
                     });
                }else{
                  this.$notify({title:"保存失败",message:'至少要有一个用户'})
                }
            },
            setCaptain:function (selectAble,index) {
                var $this =this;
                this.currentService.selectAbleUser.forEach(function (data,i) {
                    if(data.serviceGroup==selectAble.serviceGroup){
                        data.main=false;
                        $this.$set($this.currentService.selectAbleUser,i,data)
                    }
                });
                selectAble.main=true;
                this.$set(this.currentService.selectAbleUser,index,selectAble)
            },
            group:function (item) {
                this.currentService =item;
                 var groups =item.selectAbleUser.length/item.scheduleBaseNum;
                 item.groups = [];
                 for(i=1;i<groups+1;i++){
                     item.groups.push({value:i+"组",label:i+"组"});
                 }
                this.groupdialog=true;

            },
            groupComplate:function () {
                var $this = this;
                this.currentService.selectAbleUser.forEach(function (data) {
                    data.hasGroup=true;
                })
                this.groupdialog=false;
                axios.post(basepath+'/api/flightPlane/saveScheduleItemSelectabelUser',this.currentService.selectAbleUser)
                    .then(function (response) {
                        $this.$notify({
                            title: '配置成功',
                            message: '配置成功'
                        });
                    });
            }
        },

        data:function () {
            return {
                addItem: {
                    itemName: '',
                    scheduleBaseNum: '',
                    edit:false,
                    importOrExport:'',
                    timeStrategy:'',
                    beforeOrAfter:'',
                    difference:'',
                    selectAbleUser:[]

                },
                currentService:{},
                serviceItems:[],
                chooseRoomId:'',
                groupdialog:false,
                dialog:false,
                filter:"",
                cardWidth: 500,
                pointer:false,
                transferResult:[],
                allUser:[],
                allTeams:[]
            };
        },
        created:function (){
            this.searchAll();
        },
        activated:function () {
            if(this.parentData){
                this.pointer=true;
                if(this.parentData.selectRoom){
                    this.chooseRoomId=this.parentData.selectRoom.roomId;
                }

            }else{
                messages.$emit('change','地服项目配置');
                var $this= this;
                axios.get(basepath+"/api/webframe/fwteam/queryAllTeamsNopage").then(function (response) {
                    $this.allUser=response.data;
                    // response.data.forEach(function (data) {
                    //     data.user.selectableUsersId = data.serviceItemToSelectableUsersPos?data.serviceItemToSelectableUsersPos.selectableUsersId:"";
                    //     data.user.serviceTime = data.serviceItemToSelectableUsersPos?data.serviceItemToSelectableUsersPos.serviceTime:"";
                    //     $this.allUser.push(data.user);
                    // })

                });

            }

        }


    });
    module.exports = VueComponent;
    return VueComponent;

})















